package com.example.lenovo.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.Window;

import android.widget.Button;
import android.widget.EditText;

import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Lenovo on 2018/7/2.
 */

public class UpdateOrReadActivity extends Activity{
    private TextView mComplete;
    private Button mBackBtn;
    private EditText mContent;
    private LinearLayout mScreen;

    private int noteId;
    private String noteTime;
    private String noteContent;
    private String originData;

    private NoteDB mNoteDB;
    private static Note note;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.write_note);

        Intent intent = getIntent();

        noteId = intent.getIntExtra("note_id",0);
        Log.d("Anonymous", "传递后的备忘录ID:" + noteId);

        initView();

        new LoadAsyncTask().execute();
        initEvent();
    }

    private void initView() {

        mComplete = (TextView) findViewById(R.id.complete_btn);
        mBackBtn =  (Button)findViewById(R.id.back_btn);
        mContent = (EditText) findViewById(R.id.note_content);
        mScreen = (LinearLayout) findViewById(R.id.screen_view);

        mNoteDB = NoteDB.getInstance(this);
    }

    private void initEvent() {

        mBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateDataOrNot();
            }
        });

        mComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mContent.getText().toString().trim().equals("")){

                    new DeleteAsyncTask(mNoteDB).execute(noteId);
                    finish();
                } else if (mContent.getText().toString().equals(originData)) {
                    finish();
                } else {

                    new UpdateAsyncTask().execute();

                    finish();
                }
            }
        });


    }


    class LoadAsyncTask extends AsyncTask<Void,Void,Note> {

        @Override
        protected Note doInBackground(Void... voids) {
            note = mNoteDB.loadById(noteId);
            return note;
        }

        @Override
        protected void onPostExecute(Note note) {
            super.onPostExecute(note);

            originData = note.getContent();
            mContent.setText(note.getContent());
            mContent.setSelection(mContent.getText().toString().length());
        }
    }

    class UpdateAsyncTask extends AsyncTask<Void,Void,Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
            Date date = new Date(System.currentTimeMillis());
            noteTime = sdf.format(date);
            noteContent = mContent.getText().toString();
            note.setTime(noteTime);
            note.setContent(noteContent);
        }

        @Override
        protected Void doInBackground(Void... voids) {
            mNoteDB.updateById(noteTime, noteContent, noteId);
            return null;
        }
    }

    private void updateDataOrNot() {
        if (!mContent.getText().toString().equals(originData)) {
            new AlertDialog.Builder(UpdateOrReadActivity.this)
                    .setTitle("提示")
                    .setMessage("需要保存您编辑的内容吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            new UpdateAsyncTask().execute();
                            finish();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .show();
        } else {
            finish();
        }
    }
    @Override
    public void onBackPressed() {
        updateDataOrNot();
    }

}
